package com.appleyk.mybatis.v1.scanner;

import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.ClassMetadata;
import org.springframework.core.type.classreading.MetadataReader;

import java.io.IOException;
import java.util.Set;

/**
 * <p>越努力，越幸运</p>
 *
 * @author appleyk
 * @version V.0.1.1
 * @blob https://blog.csdn.net/appleyk
 * @date created on  1:04 上午 2020/12/12
 */
public class MyClassPathMapperScan extends ClassPathBeanDefinitionScanner {

    public MyClassPathMapperScan(BeanDefinitionRegistry beanDefinitionRegistry){
        super(beanDefinitionRegistry);
    }

    @Override
    public Set<BeanDefinitionHolder> doScan(String... basePackages) {
        Set<BeanDefinitionHolder> beanDefinitionHolders = super.doScan(basePackages);
        return beanDefinitionHolders;
    }

    @Override
    protected boolean isCandidateComponent(MetadataReader metadataReader) throws IOException {
        // 重写父类方法，当是mapper是接口时，也把它当成是bean组件返回
        ClassMetadata classMetadata = metadataReader.getClassMetadata();
        System.out.printf("=== classMetadata.className:%s,是否是接口：%b\n",classMetadata.getClassName(),classMetadata.isInterface());
        return metadataReader.getClassMetadata().isInterface();
    }

    @Override
    protected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition) {
        // 重写父类方法，当是mapper是接口时，也把它当成是bean组件返回
        AnnotationMetadata metadata = beanDefinition.getMetadata();
        System.out.printf("=== metadata.className:%s,是否是接口：%b\n",metadata.getClassName(),metadata.isInterface());
        return beanDefinition.getMetadata().isInterface();
    }
}
